﻿using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.Contract;
using Shop.Model.Entity.Use;
using Shop.Model.View.Admin.Contract;
using SqlSugar;

namespace Shop.Services.Admin;

public class ApproveAContractService:AbstractService, IApproveAContractService
{
    private readonly RepositoryBase<UseUser> _userRepository;

    public ApproveAContractService(RepositoryBase<UseUser> userRepository)
    {
        _userRepository = userRepository;
    }

    /// <summary>
    /// 合同列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public AppResult<PageModel<ContractListVo>> ContractSearch(ContractPageSearchDto input)
    {
        var records = 0;
        var data = _userRepository.Queryable<UserContract, UseUser>((t1, t2) => new object[]
             {
                JoinType.Left, t1.UserId == t2.Id
             }).WhereIF(input.StartTime.HasValue, (t1, t2) => t1.CreatTime > input.StartTime.Value)
            .WhereIF(input.EndTime.HasValue, (t1, t2) => t1.CreatTime < input.EndTime.Value)
            .WhereIF(input.State.HasValue, (t1, t2) => t1.State == input.State.Value)
            .WhereIF(input.UId.HasValue, (t1, t2) => t2.Code == input.UId.Value)
            .OrderBy("t1.CreatTime DESC")
            .Select((t1, t2) => new ContractListVo
            {
                Id = t1.Id,
                Code = t1.Code,
                UserId = t2.Id,
                UserName = t2.Nickname,
                UserCode = t2.Code,
                Tele = t2.Tele,
                Contract = t1.Contract,
                IDCard = t1.IDCard,
                IDCardBack = t1.IDCardBack,
                CreatTime = t1.CreatTime,
                State = t1.State
            }).ToPageList(input.PageIndex, input.PageSize,ref records);
        return new PageModel<ContractListVo>(input, data, records);
    }

    /// <summary>
    /// 合同审核
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<AppResult> ContractReview(ContractReviewDto input)
    {
        var contract = await _userRepository.Queryable<UserContract>().FirstAsync(x => x.Id == input.Id);
        if (contract == null)
            return new AppResult(404,"找不到合同");
        if(contract.State != 0)
            return new AppResult(404, "重复审核");
        var user = await _userRepository.GetByIdAsync(contract.UserId);
        if (input.Result == -1)
        {
            contract.State = -1;
            await _userRepository.Change<UserContract>().UpdateAsync(contract);
            _userRepository.AsUpdateable().Where(x => x.Id == user.Id)
                .SetColumns(x => x.Audit == 1)
                .ExecuteCommand();
        }
        else if (input.Result == 1)
        {
            if (user.Grade != 3)
            {
                user.Grade = 3;
                _userRepository.AsUpdateable().Where(x => x.Id == user.Id)
                    .SetColumns(x => x.Grade == 3)
                    .SetColumns(x => x.Audit == 3)
                    .ExecuteCommand();
                await _userRepository.Change<UserGradeLog>().InsertAsync(new UserGradeLog
                {
                    UserId = user.Id,
                    ChangeFrontGrade = user.Grade,
                    ChangeLaterGrade = 3,
                    Title = "推广升级"
                });
            }
            contract.State = 1;
            await _userRepository.Change<UserContract>().UpdateAsync(contract);
        }
        return new AppResult();
    }

    /// <summary>
    /// 合同详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<ContractVo> ContractInfo(long dto)
    {
        var data = _userRepository.Queryable<UserContract, UseUser>((t1, t2) => new object[]
            {
                JoinType.Left, t1.UserId == t2.Id
            })
            .Where((t1, t2) => t1.Id == dto)
            .Select((t1, t2) => new ContractVo()
            {
                Id = t1.Id,
                Code = t2.Code,
                Name = t2.Nickname,
                Tele = t2.Tele,
                Contract = t1.Contract,
                IDCard = t1.IDCard,
                IDCardBack = t1.IDCardBack,
                State = t1.State,
                CreatTime = t1.CreatTime
            })
            .First();
        return data;
    }
}